package entity

import (
	"slices"

	"github.com/lab-online/internal/shared/valueobject"
	"github.com/lab-online/pkg/auth"
)

func (c *Class) CanHandleClass(i auth.AuthEntity, readOnly bool) bool {
	isOwner := i.IsAdmin() || i.IsSelf(c.Teacher.GetUsername())

	if readOnly {
		return isOwner || c.ContainsStudent(i.GetUsername())
	} else {
		return isOwner
	}
}

func (c *Class) ContainsStudent(studentUsername string) bool {
	return slices.ContainsFunc(c.Students, func(student valueobject.UserVO) bool {
		return student.GetUsername() == studentUsername
	})
}
